import { Layers, Node, SpriteFrame, UITransform } from 'cc'


export const createUINode = (name: string = '', parent?: Node) => {
    const node = new Node(name);
    node.setParent(parent);
    const transform = node.addComponent(UITransform);
    transform.setAnchorPoint(0, 1);
    node.layer = Layers.Enum.UI_2D;
    return node;
}
export const randomBylen = (len: number) =>
    Array.from({ length: len }).reduce<string>((total: string, item) => total + Math.floor(Math.random() * 10), '');

export const randomByRange = (start: number, end: number) => Math.floor(start + (end - start) * Math.random());

const reg = /\((\d+)\)/;

const getNumberWithinString = (str: string) => parseInt(str.match(reg)[1] || '0');

export const sortSpriteFrame = (spriteFrames: SpriteFrame[]) =>
    spriteFrames.sort((a: SpriteFrame, b: SpriteFrame) => getNumberWithinString(a.name) - getNumberWithinString(b.name));
